package com.uzhie.jt808.service.impl;

import com.uzhie.exception.JT_8702Exception;
import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.JT808Session;
import com.uzhie.jt808.JT808SessionManager;
import com.uzhie.jt808.msg.JT808MsgBean;
import com.uzhie.jt808.service.BaseService;
import com.uzhie.jt808.service.JT808InfoService;
import com.uzhie.jt808.spring.MsgIDMapping;
import com.uzhie.jt808.spring.MsgService;
import com.uzhie.web.service.JT_8702Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * Created by TangFei on 2018/8/9.
 * 信息采集类型协议
 */
@MsgService(name = "info")
public class JT808InfoServiceImpl extends BaseService implements JT808InfoService {

    @Autowired(required = false)
    private JT_8702Service jt8702Service;

    @Autowired
    @Qualifier(value = "sessionManager")
    protected JT808SessionManager sessionManager;

    /**
     * 8.48 驾驶员身份信息采集上报
     *
     * @param msgBean
     * @return
     */
    @Override
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_RES_DRIVER_INFO)
    public JT808MsgBean devDriverInfo(JT808MsgBean msgBean) {
        logger.trace("驾驶员身份信息采集上报");
        try {
            jt8702Service.addJT8702(msgBean.getBodyBytes());
        } catch (JT_8702Exception e) {
            logger.error(msgBean.getHeader().getHexMsgID() + " 指令异常", e);
        } finally {
            // 设置驾驶员信息
            JT808Session session = sessionManager.getSession(msgBean.getHeader().getSimNum());
            if (JT808Session.PROTO.PROTO_2011.equals(session.getProto())) {
                logger.trace("SIM session判断 执行协议 2011");
                session.setDriverName(getDriverName2011(msgBean.getBodyBytes()));
            } else if (JT808Session.PROTO.PROTO_2013.equals(session.getProto())) {
                logger.trace("SIM session判断 执行协议 2013");
                session.setDriverName(getDriverName2013(msgBean.getBodyBytes()));
            } else {
                if (msgBean.getBodyBytes().length > 61) {
                    logger.trace("SIM 消息体长度判断 执行协议 2011");
                    session.setDriverName(getDriverName2011(msgBean.getBodyBytes()));
                } else {
                    logger.trace("SIM 消息体长度判断 执行协议 2013");
                    session.setDriverName(getDriverName2013(msgBean.getBodyBytes()));
                }
            }
            logger.trace("SIM：" + msgBean.getHeader().getSimNum() + " 设置了驾驶员信息：" + session.getDriverName());
            sessionManager.setSession(msgBean.getHeader().getSimNum(), session);
            // 响应通用应答
            byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
            return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_COMMON, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
        }
    }

    /**
     * 获取驾驶员姓名 2013
     *
     * @param bodys
     * @return
     */
    private String getDriverName2013(byte[] bodys) {
        String name = null;
        ByteBuffer wrap = ByteBuffer.wrap(bodys);
        byte result = wrap.get(7);
        if (0x00 == result) {
            int nameLength = wrap.get(8) & 0XFF;
            byte[] nameBytes = new byte[nameLength];
            wrap.position(9);
            wrap.get(nameBytes, 0, nameLength);
            name = new String(nameBytes, Charset.forName("gbk"));
        } else {
            logger.error("IC 卡读卡失败");
        }
        return name;
    }

    /**
     * 获取驾驶员姓名 2011
     *
     * @param bodys
     * @return
     */
    private String getDriverName2011(byte[] bodys) {
        String name = null;
        ByteBuffer wrap = ByteBuffer.wrap(bodys);
        int nameLength = wrap.get() & 0XFF;
        byte[] nameBytes = new byte[nameLength];
        wrap.position(1);
        wrap.get(nameBytes, 0, nameLength);
        name = new String(nameBytes, Charset.forName("gbk"));
        return name;
    }

}
